<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录</view>
			<view class="form-input">
				<u-input v-model="email" type="text" :border="border" placeholder="请输入邮箱" class="input"/>
			</view>
			<view class="form-input">
				<u-input v-model="psd" :type="type" :border="border" :password-icon="passwordIcon" placeholder="请输入密码" />
			</view>
			<!-- <view class="tips">未注册的手机号验证后自动创建美团账号</view> -->
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">登陆</button>
			<view class="alternative">
				<view class="password" @click="forgetPassword()">忘记密码？</view>
				<view class="issue" @click="changeRoute1()">注册</view>
			</view>
		</view>
		<statement :text="text"></statement>
	</view>
</template>

<script>
	import statement from '../../commponts/statement.vue'
	export default {
		components:{
			statement
		},
		data() {
			return {
				email: '',
				psd: '',
				type: 'password',
				passwordIcon: true,
				border: true,
				UserInfo:[],
				text:"登陆代表同意"
			}
		},
		computed: {
			inputStyle() {
				let style = {};
				if (this.$u.test.email(this.email) && this.psd) {
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			}
		},
		methods: {
			async submit() {
				// 当用户输入邮箱与密码满足前台校验时,请求接口校验
				if (this.$u.test.email(this.email) && this.psd) {
					console.log(this.email)
					const params = {
						email: this.email,
						password: this.psd
					}
					await this.$u.post('/api/auth/login', params).then(res => {
						// 缓存token
						this.$u.vuex('vuex_token', res.data.access_token);
						uni.showToast({
							title:"登陆成功",
							icon:'success'
						})
						// 拿到缓存的页面路径
					    const backUrl=uni.getStorageSync('back_url') || 'pages/index/index'
						setTimeout(res=>{
							uni.$u.route({
								type: 'reLaunch',
								url: backUrl
							})
						},1000)
					}).catch(err => {
						console.log(err)
					})
					// uni.$u.route({
					// 	type: 'switchTab',
					// 	url: 'pages/me/me'
					// })
				}
				
				// 根据用户的token请求用户的信息
				await this.$u.get('/api/user').then(res=>{
					console.log(res)
					this.UserInfo=res.data
					// 缓存用户信息
					this.$u.vuex('vuex_user',this.UserInfo)
				}).catch(err=>{
					console.log(err)
				})
			},
			forgetPassword() {
				this.$u.route({
					url: '/pages/auth/password'
				})
			},
			changeRoute1() {
				this.$u.route({
					url: 'pages/auth/register'
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.form-input{
		padding-bottom: 40rpx;
	}
	.u-border-bottom {
		margin-bottom: 50rpx !important;
	}
	
	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}
	}
</style>
